package com.atguigu.gmall.realtime.util;

import org.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @Author lizhenchao@atguigu.cn
 * @Date 2021/3/24 14:23
 */
public class MyKeyWordUtil {
    public static List<String> analyzer(String text) {
        StringReader reader = new StringReader(text);
        final IKSegmenter ikSegmenter = new IKSegmenter(reader, true); // smart max_word  我是中国人
        try {
            final List<String> result = new ArrayList<>();
            Lexeme next = ikSegmenter.next();
            while (next != null) {
                final String word = next.getLexemeText();
                result.add(word);
                next = ikSegmenter.next();
            }
            
            // 把关键字列表去重
            final Set<String> set = new HashSet<>(result);
            result.clear();
            result.addAll(set);
    
            return result;
            
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    
    public static void main(String[] args) {
        System.out.println(analyzer("我是中国人"));
    }
}

